﻿using Iworld.Backstage.Framework.AdministratorService;
using Iworld.Backstage.Framework.AuthorService;
using System;
using System.Collections.ObjectModel;
using System.Linq;
namespace Iworld.Backstage.Framework
{
    /// <summary>
    /// 用户帐变记录
    /// </summary>
    public class AuthorGoldChangeRecordsViewModel : ShowListViewModelBase<AuthorGoldsChangeRecordExport, AuthorServiceClient>
    {
          DateTime? beginTime = null;
          DateTime? endTime = null;
          string keyword = null;

          ObservableCollection<FilterBoxModel> typeGroups = new ObservableCollection<FilterBoxModel>();

        /// <summary>
        /// 帐变类型
        /// </summary>
          public ObservableCollection<FilterBoxModel> TypeGroups
          {
              get { return typeGroups; }
              set
              {
                  if (typeGroups == value) { return; }
                  typeGroups = value;
                  OnPropertyChanged("TypeGroups");
              }
          }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? BeginTime
        {
            get
            {
                return beginTime;
            }
            set
            {
                if (beginTime != value)
                {
                    beginTime = value;
                    OnPropertyChanged("BeginTime");
                    Refresh(1);
                }
            }
        }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime
        {
            get
            {
                return endTime;
            }
            set
            {
                if (endTime != value)
                {
                    endTime = value;
                    OnPropertyChanged("EndTime");
                    Refresh(1);
                }
            }
        }

        /// <summary>
        /// 关键字
        /// </summary>
        public string Keyword
        {
            get
            {
                return keyword;
            }
            set
            {
                if (keyword != value)
                {
                    keyword = value;
                    OnPropertyChanged("Keyword");
                    Refresh(1);
                }
            }
        }

        public AuthorGoldChangeRecordsViewModel()
            : base(PageCategory.用户管理.ToString(), Page.用户帐变记录.ToString())
        {
            this.client.GetGoldsChangeRecordsCompleted += this.WrapEventHandler<GetGoldsChangeRecordsCompletedEventArgs>(GetGoldsChangeRecordsCompleted);
            TypeGroups.Add(new FilterBoxModel("全部", null, Refresh, true));
            TypeGroups.Add(new FilterBoxModel("压分", "压分", Refresh));
            TypeGroups.Add(new FilterBoxModel("中奖", "中奖", Refresh));
            TypeGroups.Add(new FilterBoxModel("赠送", "赠送", Refresh));
            TypeGroups.Add(new FilterBoxModel("被赠送", "被赠送", Refresh));
            TypeGroups.Add(new FilterBoxModel("充值", "充值", Refresh));
            TypeGroups.Add(new FilterBoxModel("提现", "提现", Refresh));
            TypeGroups.Add(new FilterBoxModel("提现失败", "提现失败", Refresh));
        }

        void GetGoldsChangeRecordsCompleted(object sender, GetGoldsChangeRecordsCompletedEventArgs e)
        {
            if (e.Result.Success)
            {
                this.PageIndex = e.Result.PageIndex;
                this.TotalPage = e.Result.CountOfPage;
                UpdateLsit(e.Result.List);
            }
            else
            {
                ShowError(e.Result.Error);
            }
        }

        protected override void Refresh(object obj)
        {

            int pageIndex = obj == null ? this.PageIndex : Convert.ToInt32(obj);
            client.GetGoldsChangeRecordsAsync(new GetGoldsChangeRecordImport()
            { 
                BeginTime = this.BeginTime,
                EndTime = this.EndTime,
                PageIndex = pageIndex,
                Keyword = this.Keyword,  
                Type=(string)this.TypeGroups.First(x => x.Selected).Value,
                Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
            });
        }

        protected override void Reset(object obj)
        {
            this.BeginTime = null;
            this.EndTime = null;
            this.PageIndex = 1;
            Refresh(null);
        }
    }
}
